$(function () {
});

$('#checkFailPage').on('hidden.bs.modal', function () {
    $('#remark').val('');
});

function passOrg() {
    var str = '确定通过审核吗？';
    swalToastr('审核通过', str, 'warning', doCheckMergeOrg);
}

//校验机构是否需要合并
function doCheckMergeOrg() {
    var postData = {
        orgName : oneAfterOrgInfo.orgName,
        orgCode : oneAfterOrgInfo.orgCode,
        orgMergeCode : oneAfterOrgInfo.orgMergeCode,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'checkOrg/checkMergeOrg?t=' + new Date().getTime(),
        dataType: 'json',
        data: postData,
        success : function (data) {
            if(data && data.result){
                if (data.code == 'EXIST') {
                    //存在已发布数据，合并
                    doOpenMergePage();
                } else {
                    //不存在已发布数据，不合并，直接审核通过
                    doCheckPassOrg();
                }
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error(common_system_error);
            }
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

//打开合并校验页
function doOpenMergePage() {
    $('#_sOrgName').html(oneAfterOrgInfo.orgName);
    $('#publishOrgTable').bootstrapTable('refresh', {
        url: basePath + 'checkOrg/findPublishOrgData?t=' + new Date().getTime(),
        query: {
            orgName : oneAfterOrgInfo.orgName,
            orgCode : oneAfterOrgInfo.orgCode,
            orgMergeCode : oneAfterOrgInfo.orgMergeCode,
        },
    });
    $('#mergePage').modal('show');
}

//关闭合并校验页
function closeMergePage() {
    $('#mergePage').modal('hide');
    $('#_sOrgName').html('');
    $('#_publishOrgDataList').html('');
}

//审核通过
function doCheckPassOrg() {
    var postData = {
        id : oneLogInfo.id,
        orgCode : oneAfterOrgInfo.orgCode,
        remark : '审核通过',
    };
    showLoading();
    $.ajax({
        type: 'post',
        url: basePath + 'checkOrg/checkPassOrg?t=' + new Date().getTime(),
        dataType: 'json',
        data: postData,
        success : function (data) {
            hideLoading();
            if (!data) {
                toastr.error(common_system_error);
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            doClose();
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

//合并机构
function doMergeOrg() {
    var rows = getSelectedRows("#publishOrgTable");
    if (!rows || rows.length == 0) {
        toastr.error('请选择要合并的数据项');
        return;
    }
    if (rows.length != 1) {
        toastr.error('所选数据存在异常，请刷新页面后重试');
        return;
    }
    var remark = '审核通过，' + oneAfterOrgInfo.orgName + '[' + oneAfterOrgInfo.orgCode + ']合并至' + rows[0].orgName + '[' + rows[0].orgCode + ']';
    var postData = {
        id : oneLogInfo.id,
        publishOrgCode : rows[0].orgCode,
        remark : remark,
    };
    showLoading();
    $.ajax({
        type: 'post',
        url: basePath + 'checkOrg/checkPassOrgWithMerge?t=' + new Date().getTime(),
        dataType: 'json',
        data: postData,
        success : function (data) {
            hideLoading();
            if (!data) {
                toastr.error(common_system_error);
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            doClose();
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

//不合并机构
function doNotMergeOrg() {
    doCheckPassOrg();
}

//打开审核不通过页
function openCheckFailPage() {
    $('#checkFailPage').modal('show');
}

//关闭审核不通过页
function closeCheckFailPage() {
    $('#checkFailPage').modal('hide');
    $('#remark').val('');
}

//审核不通过
function unPassOrg() {
    var remark = $('#remark').val();
    if (!remark || remark == '') {
        toastr.error('原因不能为空');
        return;
    }
    var postData = {
        id : oneLogInfo.id,
        bizCode : oneAfterOrgInfo.orgCode,
        remark : remark
    };
    showLoading();
    $.ajax({
        type: 'post',
        url: basePath + 'checkOrg/checkFailOrg?t=' + new Date().getTime(),
        dataType: 'json',
        data: postData,
        success : function (data) {
            hideLoading();
            if (!data) {
                toastr.error(common_system_error);
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            doClose();
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

//返回列表页
function doClose() {
    var str = '&orgName=' + searchLogInfo.orgName + '&checkType=' + searchLogInfo.checkType + '&isvPid=' + searchLogInfo.isvPid +
              '&isvName=' + searchLogInfo.isvName + '&province=' + searchLogInfo.province + '&city=' + searchLogInfo.city +
              '&orgCategory=' + searchLogInfo.orgCategory + '&beginTimeStr=' + searchLogInfo.beginTimeStr +
              '&isQueryExist=' + searchLogInfo.isQueryExist + '&isCheckExist=' + searchLogInfo.isCheckExist +
              '&pageSize=' + searchLogInfo.pageSize + '&pageNo=' + searchLogInfo.pageNo + '&timeInterval=' + searchLogInfo.timeInterval;
    if (defType == 'query') {
        window.location.href = basePath + 'checkOrg/queryOrgPage?t=' + new Date().getTime() + str + '&result=' + searchLogInfo.result;
    } else {
        window.location.href = basePath + 'checkOrg/checkOrgPage?t=' + new Date().getTime() + str;
    }
}

function formatAddress(value, row, index) {

    return (row.province?row.province:'') + (row.city?row.city:'') + (row.area?row.area:'');
}